<template>
  <div class="login-container">
    <div class="login-box">
      <h2>系统登录</h2>
      <div class="input-group">
        <input 
          type="text" 
          v-model="loginForm.phone"
          placeholder="请输入用户名"
          @keyup.enter="handleLogin"
        >
        <div class="error-tip" v-if="errors.phone">{{ errors.phone }}</div>
      </div>
      <div class="input-group">
        <input 
          type="password" 
          v-model="loginForm.password" 
          placeholder="请输入密码"
          @keyup.enter="handleLogin"
        >
        <div class="error-tip" v-if="errors.password">{{ errors.password }}</div>
      </div>
      
      <div class="remember-me">
        <input type="checkbox" id="remember" v-model="rememberMe">
        <label for="remember">记住我</label>
      </div>

      <button @click="handleLogin" class="login-button" :disabled="loading">
        {{ loading ? '登录中...' : '登录' }}
      </button>
    </div>
  </div>
</template>

<script>
export default {
  name: 'Login',
  data() {
    return {
      loginForm: {
        phone: '',
        password: ''
      },
      errors: {
        phone: '',
        password: ''
      },
      loading: false,
      rememberMe: false,
      type: 'admin' // 默认选中管理员
    }
  },
  created() {
    // 检查本地存储中是否有保存的用户名
    const savedPhone = localStorage.getItem('rememberedPhone')
    if (savedPhone) {
      this.loginForm.phone = savedPhone
      this.rememberMe = true
    }
  },
  methods: {
    validateForm() {
      let isValid = true
      this.errors = {
        phone: '',
        password: ''
      }
      
      if (!this.loginForm.phone) {
        this.errors.phone = '请输入用户名'
        isValid = false
      }
      
      if (!this.loginForm.password) {
        this.errors.password = '请输入密码'
        isValid = false
      }
      
      return isValid
    },
    handleLogin() {
      if (!this.validateForm()) {
        return
      }
      
      this.loading = true
      
      this.$http.post("/platform/account/accountLogin", this.loginForm).then(resp => {
        if(resp.data.code == 200) {
          // 如果选择了记住我，保存用户名到本地存储
          if (this.rememberMe) {
            localStorage.setItem('rememberedPhone', this.loginForm.phone)
          } else {
            localStorage.removeItem('rememberedPhone')
          }
          
          this.$message.success("登录成功")
          // 存储用户信息和角色
          localStorage.setItem('userInfo', JSON.stringify(resp.data.data))
          localStorage.setItem('roleName', resp.data.data.roleName)
          
          // 根据用户角色跳转到不同页面
          const roleName = resp.data.data.roleName
          if(roleName === '站点管理员' || roleName === '客服') {
            this.$router.replace('/station/management')
          } else {
            this.$router.replace('/platform/stationorders')
          }
        } else {
          this.$message.error(resp.data.message || "登录失败")
        }
      }).catch(error => {
        console.error('登录失败:', error)
        this.$message.error("登录失败，请稍后重试")
      })
    }
  }
}
</script>

<style scoped>
.login-container {
  display: flex;
  justify-content: center;
  align-items: center;
  height: 100vh;
  background-color: #f5f5f5;
}

.login-box {
  width: 400px;
  padding: 40px;
  background: white;
  border-radius: 8px;
  box-shadow: 0 2px 12px rgba(0, 0, 0, 0.1);
}

h2 {
  text-align: center;
  margin-bottom: 30px;
  color: #333;
}

.input-group {
  margin-bottom: 20px;
  position: relative;
}

input {
  width: 100%;
  padding: 12px;
  border: 1px solid #dcdfe6;
  border-radius: 4px;
  font-size: 14px;
  transition: all 0.3s;
}

input:focus {
  outline: none;
  border-color: #409eff;
}

.login-button {
  width: 100%;
  padding: 12px;
  background-color: #409eff;
  color: white;
  border: none;
  border-radius: 4px;
  cursor: pointer;
  font-size: 14px;
  transition: all 0.3s;
}

.login-button:hover:not(:disabled) {
  background-color: #66b1ff;
}

.login-button:disabled {
  background-color: #a0cfff;
  cursor: not-allowed;
}

.error-tip {
  color: #f56c6c;
  font-size: 12px;
  margin-top: 5px;
}

.remember-me {
  display: flex;
  align-items: center;
  margin-bottom: 20px;
}

.remember-me input {
  width: auto;
  margin-right: 8px;
}
</style> 